package com.ffms.servlet;

import com.ffms.dao.impl.ShouruDaoImpl;
import com.ffms.entity.Shouru;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UpdateIncomeServlet")
public class UpdateIncomeServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        //1.接收视图层传来的表单数据并封装成为对象
        String id = request.getParameter("id");
        String from = request.getParameter("from");
        String money = request.getParameter("money");
        String way = request.getParameter("way");
        Shouru shouru = new Shouru(Integer.parseInt(id), from, money, way);

        ShouruDaoImpl shouruDao = new ShouruDaoImpl();
        int n = shouruDao.updateShouru(shouru);
        //3 根据结果跳转到不同页面
        if(n>0){
            //成功则挑转到主页面
            request.getRequestDispatcher("/AllShouruServlet").forward(request, response);
        }else{
            //失败则提示
            request.setAttribute("error", "更新s收入失败");
            request.getRequestDispatcher("/IncomeServlet?id="+id).forward(request, response);
        }
    }
}
